#!/bin/sh

if [ -n "${GNUPATH}" ]; then
	PATH="${GNUPATH}:${PATH}"
fi

if [ -x "$(which gnufind 2>/dev/null)" ]; then
	FIND="gnufind"
elif [ -x "$(which gfind 2>/dev/null)" ]; then
	FIND="gfind"
elif [ -x "$(which find 2>/dev/null)" ]; then
	FIND="find"
else
	return
fi

if [ -x "$(which gnuindent 2>/dev/null)" ]; then
	INDENT="gnuindent"
elif [ -x "$(which gindent 2>/dev/null)" ]; then
	INDENT="gindent"
elif [ -x "$(which indent 2>/dev/null)" ]; then
	INDENT="indent"
else
	return
fi

if [ -x "$(which CodeClean 2>/dev/null)" ]; then
	CODECLEAN="CodeClean"
fi

FILELIST="`${FIND} $@ -iname \"*.c\" -or -iname \"*.h\" -or -iname \"*.cpp\"`"
for FILE in ${FILELIST}
do
	echo "Indent ${FILE}"
	PARAM="-npro -kr -nut -as -i4 -ts4 -sob -l80 -ss -ncs -cp1"
	RES=`${INDENT} --version`
	V1=`echo ${RES} | cut -d' ' -f3 | cut -d'.' -f1`
	V2=`echo ${RES} | cut -d' ' -f3 | cut -d'.' -f2`
	V3=`echo ${RES} | cut -d' ' -f3 | cut -d'.' -f3`
	if [ ${V1} -gt 2 ]; then
		PARAM="${PARAM} -il0"
	elif [ ${V1} -eq 2 ]; then
		if [ ${V2} -gt 2 ]; then
			PARAM="${PARAM} -il0";
		elif [ ${V2} -eq 2 ]; then
			if [ ${V3} -ge 10 ]; then
				PARAM="${PARAM} -il0"
			fi
		fi
	fi
	if [ -n "${CODECLEAN}" ]; then
		${CODECLEAN} ${FILE}
	fi
	${INDENT} ${PARAM} ${FILE}
done

